/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    form.render(null, "zmn-point-form");

    zmnPage.laySelectLinkage2(form, {
        "cityId" : "/common/area/listCompanyCity.action",
        "countyId" : "/common/area/listChilds.action"
    }, "companyId", "cityId", "countyId");

    // 自定义验证规则
    form.verify({
        title: function (value) {
            if (value.length < 2) {
                return "不能少于2个字符";
            }
            ;
        }
    });

    // 提交
    form.on('submit(zmn-point-form-submit)', function (obj) {
        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/point/addEdit.action";

        var pointAreas = document.getElementsByName("areaIds");

        var ids = "";
        for (var i in pointAreas) {
            var pa = pointAreas[i];
            if (pa.checked == true) {
                if (ids != "") {
                    ids += ",";
                }
                ids += pa.value;
            }
        }

        data.areaIds = ids;

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (json) {
                layer.close(openId);
                parent.layui.layer.msg(json.message);
                if (json.status == 200 && json.id > 0) {
                    parent.layui.element.tabChange('layadmin-layout-tabs', "/point/list.action");
                    parent.layui.element.tabDelete('layadmin-layout-tabs', window.location.pathname + window.location.search);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.close(openId);
            }
        })

        return false;
    });

    // 网点覆盖区
    form.on('select(countyId)', function(data){
        loadPointArea();
    });

    if ($("#cityId").val() > 0) {
        loadPointArea();
    }

    //对外暴露的接口
    exports('art_point_detail', {});
});

function loadPointArea() {
    var $ = layui.$;
    $.ajax({
        type: "POST",
        url: "/common/point/area/listChilds.action",
        dataType: "json",
        data: {"cityId" : $("#cityId").val(), "pointId" : $("#pointId").val()},
        success: function (json) {
            var checkbox = '';
            for (var i = 0; i < json.length; i++) {
                var box = json[i];
                var cbox = '<input type="checkbox" name="areaIds" title="' + box.text +'" lay-verify="required" value="'
                    + box.value + '" lay-skin="primary" ' + (box.status == true ? 'checked' : '') + '>';
                checkbox += cbox;
            }
            document.getElementById("pointArea").innerHTML = checkbox;
            layui.form.render();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

function createMap1() {
    var longitude = layui.$("#longitude").val();
    var latitude = layui.$("#latitude").val();

    if (longitude == null || longitude == "") {
        layer.tips('请填写网点经度', '#createMapId1');
    }
    if (latitude == null || latitude == "") {
        layer.tips('请填写网点纬度', '#createMapId1');
    }

    openBaiduMap1(longitude, latitude);
}

function createMap2() {
    var longitude = layui.$("#longitude").val();
    var latitude = layui.$("#latitude").val();

    if (longitude == null || longitude == "") {
        layer.tips('请填写网点经度', '#createMapId1');
    }
    if (latitude == null || latitude == "") {
        layer.tips('请填写网点纬度', '#createMapId1');
    }

    openBaiduMap2(longitude, latitude);
}

var mapArgs = {
    point: {},
    pointData: {}
};

/**
 * iframe窗
 * 多边形地图
 * @param longitude 经度
 * @param latitude 纬度
 */
function openBaiduMap1(longitude, latitude) {
    mapArgs.point = {"longitude": longitude, "latitude": latitude};
    mapArgs.pointData = layui.$("#mapData").val();
    layer.open({
        type: 2,
        name: "mapFrame",
        title: "百度画图",
        closeBtn: 1,
        shade: [0],
        area: ['100%', '100%'],
        anim: 2,
        content: ['/static/baidu/baiduMap.html', 'no']
    });
}

/**
 * iframe窗
 * 圆形地图
 * @param longitude 经度
 * @param latitude 纬度
 */
function openBaiduMap2(longitude, latitude) {
    mapArgs.point = {"longitude": longitude, "latitude": latitude};
    mapArgs.pointData = layui.$("#mapData").val();
    layer.open({
        type: 2,
        name: "mapFrame",
        title: "百度画图",
        closeBtn: 1,
        shade: [0],
        area: ['100%', '100%'],
        anim: 2,
        content: ['/static/baidu/baiduCircleMap.html', 'no']
    });
}

/**
 * 接收子页面传来的地图数组数据
 * @param points 地图数据：多边形的时候为json, 圆形为半经
 * @param longitude 如果是画圆，则可能会修改网点坐标
 * @param latitude
 */
function submitMap(points, longitude, latitude) {
    var data = JSON.stringify(points);
    layui.$("#mapData").val(data);
    if (longitude != null && longitude != "" && latitude != null && latitude != "") {
        layui.$("#longitude").val(longitude);
        layui.$("#latitude").val(latitude);
        layui.$("input[name=mapDataType][value=1]").attr("checked", false);
        layui.$("input[name=mapDataType][value=2]").attr("checked", true);
    } else {
        layui.$("input[name=mapDataType][value=2]").attr("checked", false);
        layui.$("input[name=mapDataType][value=1]").attr("checked", true);
    }
    layui.form.render();
    layer.closeAll();
}